﻿using PLC.Interface.Groups;
using PLCWpfTest.Common;
using PLCWpfTest.Common.Extensions;
using System.Collections.Generic;
using System.Threading.Tasks;
using System.Windows;
using YWM.Dto;

namespace PLCWpfTest.ViewModels
{
    public class GroupConfigViewModel : ManagementViewModel
    {
        private readonly IGroupAppService _groupAppService;

        private IReadOnlyList<GroupResultDto> _groups;
        public IReadOnlyList<GroupResultDto> Groups
        {
            get { return _groups; }
            set { SetProperty(ref _groups, value); }
        }

        public GroupConfigViewModel(IGroupAppService groupAppService)
        {
            _groupAppService = groupAppService;
        }

        protected override async Task LoadCommand_Executing(object parameter)
        {
            await SearchAsync();
        }

        public async Task SearchAsync()
        {
            Groups = await _groupAppService.GetList(false);
        }

        protected override bool DeleteCommand_BeforeExecute(object parameter)
        {
            if (parameter == null)
            {
                this.ShowWarning("请选择一行删除");
                return false;
            }
            MessageBoxResult result = this.ShowQuestion(Lang["确定要删除吗"]);
            return result == MessageBoxResult.OK;
        }

        protected override async Task<IEntityDto> DeleteAsync(object parameter)
        {
            GroupResultDto group = (GroupResultDto)parameter;
            await _groupAppService.Delete(group);
            await SearchAsync();
            return group;
        }
    }
}
